package com.zretc.impl;

import java.util.List;

import com.zretc.dao.ZhiweiDao;
import com.zretc.entity.Users;
import com.zretc.entity.Zhiwei;
import com.zretc.tools.DBTools;
import com.zretc.tools.PageData;

public class ZhiweiDaoImpl implements ZhiweiDao {

	@Override
	public boolean updatazhiwei(Zhiwei zhiwei) {
		if (zhiwei != null) {
			String sql = "update zhiwei set bumen=?,name=?,didian=?,xuqiu=?," + "salary=?,neirong=? where gid=?";

			int n = DBTools.exUpdate(sql, zhiwei.getBumen(), zhiwei.getName(), zhiwei.getDidian(), zhiwei.getXuqiu(),
					zhiwei.getSalary(), zhiwei.getNeirong(), zhiwei.getGid());

			return n > 0;
		}
		return false;
	}

	@Override
	public boolean addZhiwei(Zhiwei z) {
		// TODO Auto-generated method stub
		String sql = "insert into zhiwei values(null,?,?,?,?,?,?)";
		return DBTools.exUpdate(sql, z.getName(), z.getBumen(), z.getDidian(), z.getXuqiu(), z.getSalary(),
				z.getNeirong()) > 0;
	}

	@Override
	public Zhiwei getZhiweiByName(String name) {
		// TODO Auto-generated method stub
		String sql = "select name,didian,xuqiu,salary,neirong,bumen from zhiwei where name= ?";
		List<Zhiwei> list = (List<Zhiwei>) DBTools.exQuery(sql, Zhiwei.class, name);

		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Zhiwei> getZhiwei(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from zhiwei where name like ?";
		List<Zhiwei> list = (List<Zhiwei>) DBTools.exQuery(sql, Zhiwei.class, "%" + keywords + "%");
		return list;
	}

	@Override
	public PageData<Zhiwei> getUsersByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		PageData<Zhiwei> pd = DBTools.exQueryByPage("select * from zhiwei where name like ?", Zhiwei.class, page,
				pageSize, "%" + keywords + "%");
		return pd;
	}

	@Override
	public boolean delzhiwei(int gid) {
		String sql = "delete from zhiwei where gid=?";
		return DBTools.exUpdate(sql, gid) > 0;
	}

	@Override
	public Zhiwei getZhiweiByBumen(String bumen) {
		String sql = "select name,didian,xuqiu,salary,neirong,bumen from zhiwei where bumen= ?";
		List<Zhiwei> list = (List<Zhiwei>) DBTools.exQuery(sql, Zhiwei.class, bumen);

		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

}
